package com.bolt.common.utils;

import com.bolt.common.security.DigestUtil;
import com.bolt.common.security.Md5Util;

/**
 * Created by michao on 2018/3/14.
 */
public class PasswordUtil {
    public static final String HASH_ALGORITHM = "MD5";
    public static final int SALT_SIZE = 8;

    public static String generateSalt() {
        byte[] salt = DigestUtil.genSalt(SALT_SIZE);
        return HexUtil.encodeHexStr(salt);
    }

    public static String entryptPassword(String rawPassword) {
        return entryptPassword(rawPassword, null);
    }

    public static String entryptPassword(String rawPassword, String salt) {
        byte[] hashPassword = null;
        if (salt == null) {
            hashPassword = Md5Util.md5(rawPassword.getBytes());
        } else {
            hashPassword = Md5Util.md5((injectPasswordSalt(rawPassword, salt)).getBytes());
        }
        return HexUtil.encodeHexStr(hashPassword);
    }

    /**
     * 对原始密码注入盐值
     */
    public static String injectPasswordSalt(String rawPassword, String salt) {
        return "{" + salt + "}" + rawPassword;
    }

}
